package presentation;

import gestores.GestorUsuario;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import utils.Encriptador;

import domain.Usuario;

public class FrontController extends HttpServlet {
	 private static final long serialVersionUID = 1L;
	 
     public void init() throws ServletException {
     }

     protected void doGet(HttpServletRequest request,
                     HttpServletResponse response) throws ServletException, IOException {
             processRequest(request, response);

     }

     protected void doPost(HttpServletRequest request,
                     HttpServletResponse response) throws ServletException, IOException {
             processRequest(request, response);
     }

     public void processRequest(HttpServletRequest request,
                     HttpServletResponse response) throws IOException, ServletException {

             if (logado(request)) {
                     String resource = request.getParameter("res");
                     RequestDispatcher d = request.getRequestDispatcher(resource);
                     if (d != null) {
                            // d.forward(request, response);  (Server side)
                    	 response.sendRedirect(resource); // (Client Side)


                     }
                     System.out.println("LOGADO ACCEDIENDO A " + resource);
             } else {
            	 if(request.getParameter("loc")!=null ){
            	 String vieneDeIndex = request.getParameter("loc");
            	 
            	 if(vieneDeIndex.equals("true")){
            		 response.sendRedirect("index.jsp?error=noPass");
            	 }
            	/* if(request.getParameter("cap")=="false"){
            		 response.sendRedirect("index.jsp?capt=noCapt");
            	 }*/
            		  }
            	 else{
            		 
            		 response.sendRedirect("control/noacceso.jsp");
            		 
                 	
            	
            	 }
             }

     }
     
     private boolean logado(HttpServletRequest request) {
     boolean logado = false;

     HttpSession session = request.getSession(false);

     String userForm = request.getParameter("login");
     String passwdForm = request.getParameter("password");

     if (session == null) {
             session = request.getSession();
             if (userForm == null || passwdForm == null
                             || userForm.length() == 0 || passwdForm.length() == 0) {
                     logado = false;

             } else {
                   
                     Integer tipoUsuario = valido(userForm, passwdForm);
                             if (tipoUsuario == 1) {
                                     logado = true;
                                     session.setAttribute("session.user", userForm);
                                     session.setAttribute("session.priv", 1);
                             } else if (tipoUsuario == 2) {
                                     logado = true;
                                     session.setAttribute("session.user", userForm);
                                     session.setAttribute("session.priv", 2);
                             } else {
                                     logado = false;
                             }
                     

             }

     } else {
             if (userForm == null || passwdForm == null) {
                     logado = true;
             } else {
                    

                     Integer tipoUsuario = valido(userForm, passwdForm);
                             if (tipoUsuario == 1) {
                                     logado = true;
                                     session.setAttribute("session.user", userForm);
                                     session.setAttribute("session.priv", 1);
                             } else if (tipoUsuario == 2) {
                                     logado = true;
                                     session.setAttribute("session.user", userForm);
                                     session.setAttribute("session.priv", 2);
                             } else {
                                     logado = false;
                             }
                     

             }
     }
     return logado;
}
     

     public Integer valido(String userForm, String passwdForm) {
             Integer res = 0;
             if (!(userForm.equals("") || passwdForm.equals(""))) {
                     GestorUsuario g = new GestorUsuario();
                     Usuario usuario = g.selectUsuarioByNickAndPass(userForm,Encriptador.convierteAmd5(passwdForm));
                     if (!(usuario.getLogin().isEmpty())
                                     && (!(usuario.getPassword().isEmpty()))) {
                             res = 1;
                             if (usuario.getPermisos()>0) {
                                     res = 2;

                             }
                     }

             }
             return res;

     }
}
